<?php
include_once ('../global.php');
require_once 'css/mydata/PHPExcel/Classes/PHPExcel.php';
$type=isset($_GET['type'])?$_GET['type']:1;
if($type==1 || $type==3){
    $where=" where is_del=0 and act_id='{$type}'";
    $sql = "SELECT * FROM `bmw_active_sign_new` {$where} ";
    $list= $db->SelectSQL($sql);
    foreach ($list as &$val){
        $sql_1="SELECT * FROM `player` WHERE pl_id=".$val['user_id'];
        $info=$db->SelectSQL($sql_1);
        $val['name']=$info?$info[0]['pl_cn_name']:"";
        $val['phone']=$info?$info[0]['pl_tele']:"";
        $val['act_text']="";
        $act_text="";
        if($val['act_id']==1){
            $val['act_text']=get_active($val['session']);
        }
        if($val['act_id']==3){
            $val['act_text']=get_jiazhang_active($val['session']);
        }
    }
    if($type==1){
        $sheet_title="冠军训练营";
    }
    if($type==3){
        $sheet_title="成长之路";
    }
}
if($type==2){
    $sheet_title="试乘试驾";
    $where=" where is_del=0 ";
    $sql = "SELECT * FROM `bmw_test_drive_sign`  {$where}";
    $list= $db->SelectSQL($sql);
    foreach ($list as &$val){
        if($val['type']==1){
            $val['name']=$val['name']."(球员家长)";
        }
        if($val['type']==2){
            $val['name']=$val['name']."(会员及嘉宾)";

        }
        $arr=gettimes($val['type'],$val['time']);
        $val['act_text']=$arr['date']." ".$arr['text'];

    }
}

$objPHPExcel=new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle($sheet_title);
// 添加数据到Excel文件
//$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello, World!');
//构建列数--根据实际需要构建即可
$letter = array('A', 'B', 'C', 'D');
//表头数组--需和列数一致
if($type==2) {
    $letter = array('A', 'B', 'C', 'D', 'E', 'F', 'G','H','I','J');
    $tableheader = array('姓名', '性别','年龄','手机号', '身份证号', '驾照', '试乘试驾', '车型', '活动', '时间');
}else{
    $tableheader = array('姓名', '手机号', '活动', '时间');
}
//填充表头信息
for ($i = 0; $i < count($tableheader); $i++) {
    $objPHPExcel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableheader[$i]");
}
$objDrawing  = new PHPExcel_Worksheet_Drawing();
//$objDrawing  = new PHPExcel_Worksheet_MemoryDrawing();
//循环填充数据
foreach ($list as $k => $v) {
    $num = $k + 1 + 1;
    if($type==2){
        $act_time=$v['create_time'];
    }else{
        $act_time=$v['act_datetime'];
    }

    if($type==2) {
    
        //设置每一列的内容
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A' . $num, $v['name'])
            ->setCellValue('B' . $num, $v['sex'])
            ->setCellValue('C' . $num, $v['age'])
            ->setCellValue('D' . $num, $v['phoneno'])
            ->setCellValue('E' . $num, "\t".$v['cardtype']."\t")
            ->setCellValue('F' . $num, "\t".$v['jiazhao']."\t")
            ->setCellValue('G' . $num, $v['selecttype'])
            ->setCellValue('H' . $num, $v['testtype'])
            ->setCellValue('I' . $num, $v['act_text'])
            ->setCellValue('J' . $num, $act_time);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(60);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(30);
    }else{
        //设置每一列的内容
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A' . $num, $v['name'])
            ->setCellValue('B' . $num, $v['phone'])
            ->setCellValue('C' . $num, $v['act_text'])
            ->setCellValue('D' . $num, $act_time);

    }
        // 将Base64字符串转换为图片资源
//        $image_data = base64_decode($v['avatar']);
//         $objDrawing->setPath($image_data);
//        // 获取图片地址
//        //$objDrawing->setPath($imageData);
//        // 设置图片存放在表格的位置
//        $objDrawing->setCoordinates('D' . $num);

    //设置行高
    $objPHPExcel->getActiveSheet()->getRowDimension($k+4)->setRowHeight(30);
}
//以下是设置宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(60);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(60);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);

ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$sheet_title.'.xlsx"');
header('Cache-Control: max-age=0');

// 保存并输出Excel文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

?>